package billrestauration.controllers;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import billrestauration.daos.MainDao;
import billrestauration.entities.Menu;
import billrestauration.entities.Reservation;
import billrestauration.extra.validation.BillRestaurationValidator;

/**
 * 
 * @author Billy
 *
 */
@Controller
public class ReservationController {
	@Autowired(required=true)
	private MainDao dao;
	@Autowired(required=true)
	private org.springframework.context.support.ReloadableResourceBundleMessageSource messageresolver;
	 
	@RequestMapping(value="/reservation/{id}",method=RequestMethod.GET)
	public String reservation(Model model,@PathVariable("id") Long id,@ModelAttribute("r")Reservation r
			 ,
			Locale locale){
		Menu m=dao.findMenuById(id);
		 
		 //menuintrouvable  menuepuise
			 if(null==m){
				 model.addAttribute("menuintrouvable", 
						 messageresolver.getMessage("menuintrouvable", null, locale));
				 return "reservationsetup";
			 }
			 else if(m.getQuantite()<=0){
				 
					 model.addAttribute("menuepuise", 
							 messageresolver.getMessage("menuepuise", null, locale));
					 return "reservationsetup";
				
			 }
			
		 
		 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	      String name = auth.getName(); //get logged in username
	 Date d=new Date();
	if(null!=r)
		  d=r.getDatereservation();
	  r=new Reservation(dao.loadUserByUsername(name), m);
		  r.setDatereservation(d);
		model.addAttribute("r", r);
		return "reservationsetup";
	}
	@RequestMapping(value="/reservation",method=RequestMethod.POST)
	public String reservationaction(Model model,@ModelAttribute("r")Reservation r, Locale locale){
		//  dao.addReservation(r);
		System.out.println(r);
		Menu m=r.getMenureserve();
		 
		 //menuintrouvable  menuepuise
			 if(null==m){
				 model.addAttribute("menuintrouvable", 
						 messageresolver.getMessage("menuintrouvable", null, locale));
				 return reservation(model,r.getId(),r , locale);
			 }
			 else if(m.getQuantite()<=0){
				 
					 model.addAttribute("menuepuise", 
							 messageresolver.getMessage("menuepuise", null, locale));
					 return reservation(model,r.getId(),r , locale);
				
			 }
		try{
			dao.addReservation(r);
		}catch(Exception e){
			String erreurajoutreservation = e.getMessage();
			model.addAttribute("erreurajoutreservation",erreurajoutreservation);
			return reservation(model,r.getId(),r , locale);
		}
		model.addAttribute("reservation", r);
		return "reservationfaite";
	}
	 
	@RequestMapping(value="/reservation",method=RequestMethod.GET)
	public String reservation( Model model){
		 model.addAttribute("menus",dao.listeMenuDuJour());
		return "reservation";
	}
}
